package org.glz.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;


@Controller
@Slf4j
public class LoginController {

//    @GetMapping("/login")
//    public String login(){
//        return "login";
//    }

    @PostMapping("/login")
    public String login(String userCode, String password, Model model){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userCode,password);
        try{
            subject.login(token);

            System.out.println(subject.getPrincipal());
//            subject.getSession().setAttribute("loginUser",subject.getPrincipal());
            return "redirect:/bb/index";
        }catch (Exception ex){
            log.error("用户登录失败",ex);
            model.addAttribute("loginError","用户名或密码输入有误");
            return "/login";
        }

    }

}
